/*
	Time.h
	Christopher Raffaele
	04.28.2008

	Time's class definitions
*/
#include"TimeDec.h" //Time class declarations

//display the first menu
void Time::showFirstMenu(){
	std::cout << "\n--------------------------------\n"
		 << "\\       Time: Convert From      \\\n"
		 << " ---------------------------------\n"
		 << " | 1 | Seconds                   |\n"
		 << " ---------------------------------\n"
		 << " | 2 | Hours                     |\n"
		 << " ---------------------------------\n"
		 << " | 3 | Days                      |\n"
		 << " ---------------------------------\n"
 		 << " | 4 | Years                     |\n"
		 << " ---------------------------------\n";
}

//display the second menu
void Time::showSecondMenu(){
	std::cout << "\n--------------------------------\n"
		 << "\\        Time: Convert To       \\\n"
		 << " ---------------------------------\n"
		 << " | 1 | Seconds                   |\n"
		 << " ---------------------------------\n"
		 << " | 2 | Hours                     |\n"
		 << " ---------------------------------\n"
		 << " | 3 | Days                      |\n"
		 << " ---------------------------------\n"
 		 << " | 4 | Years                     |\n"
		 << " ---------------------------------\n";
}

//prompt for convert from choice
void Time::start(){
	//continuously ask for units to convert from until 0 is entered
	do{
		showFirstMenu(); //show the first menu

		std::cout << "\nConvert time from (0 to return to main): ";
		std::cin >> from; //set convert from units variable

		//validate input 
		if(from > 0 && from < 5){
			char fromString[13] = {' '}; //char array to hold the units converting from

			//set fromString to the units converting from
			switch(from){
				case 1:
					strncpy(fromString,"seconds",7);
					break;
				case 2:
					strncpy(fromString,"hours",5);
					break;
				case 3:
					strncpy(fromString,"days",4);
					break;
				case 4:
					strncpy(fromString,"years",5);
					break;
			}
			showSecondMenu(); //show the second menu

			//get the convert to units
			std::cout << "\nConvert " << fromString << " to: "; 
			std::cin >> to;
			
			//validate input
			if(to > 0 && to < 5)
				getTime(); //get the amount of time to be converted
			else //number other than 1-4
				std::cout << "\nInvalid Selection\n";
		}else if(from == 0);
		else //any number other than 1 - 4
			std::cout << "\nInvalid Selection\n";
	}while(from != 0);
}

void Time::getTime(){
	std::cout << "\nEnter the amount of time to be converted: ";
	double t; //variable for the amount of time to be converted
	std::cin >> t;

	//determine what to convert to
	switch(to){
		case 1:
			toSeconds(t); //convert entered time to seconds
			break;
		case 2:
			toHours(t); //convert entered time to hours
			break;
		case 3: 
			toDays(t); //convert entered time to days
			break; 
		case 4:
			toYears(t); //convert entered time to years
			break;
	}
}

void Time::toSeconds(double time){
	//based on from units convert passed time to seconds
	switch(from){
		case 1:
			std::cout << std::endl << time << "s is " << time;
			break;
		case 2:
			std::cout << std::endl << time << "hr(s) is " << time * 3600;
			break;
		case 3:
			std::cout << std::endl << time << " day(s) is " << time * 3600 * 24;
			break;
		case 4:
			std::cout << std::endl << time << " year(s) is " << time * 3600 * 24 * 365;
			break;
	}

	std::cout << "s\n";
	system("pause"); //wait
}

void Time::toHours(double time){
	//based on from units convert passed time to hours
	switch(from){
		case 1:
			std::cout << std::endl << time << "s is " << time / 3600;
			break;
		case 2:
			std::cout << std::endl << time << "hr(s) is " << time;
			break;
		case 3:
			std::cout << std::endl << time << " day(s) is " << time * 24;
			break;
		case 4:
			std::cout << std::endl << time << " year(s) is " << time * 24 * 365;
			break;
	}

	std::cout << "hr(s)\n";
	system("pause"); //wait
}

void Time::toDays(double time){
	//based on from units convert passed time to days
	switch(from){
		case 1:
			std::cout << std::endl << time << "s is " << time / (24 * 3600);
			break;
		case 2:
			std::cout << std::endl << time << "hr(s) is " << time / 24;
			break;
		case 3:
			std::cout << std::endl << time << " day(s) is " << time;
			break;
		case 4:
			std::cout << std::endl << time << " year(s) is " << time * 365;
			break;
	}

	std::cout << " day(s)\n";
	system("pause"); //wait
}

void Time::toYears(double time){
	//based on from units convert passed time to years
	switch(from){
		case 1:
			std::cout << std::endl << time << "s is " << time / (365 * 24 * 3600);
			break;
		case 2:
			std::cout << std::endl << time << "hr(s) is " << time / (365 * 24);
			break;
		case 3:
			std::cout << std::endl << time << " day(s) is " << time / 365;
			break;
		case 4:
			std::cout << std::endl << time << " year(s) is " << time;
			break;
	}

	std::cout << " year(s)\n";
	system("pause"); //wait
}